//
//  YLCProgramPhotoInsertCell.m
//  LinYa
//
//  Created by 初程程 on 2018/2/25.
//  Copyright © 2018年 初程程. All rights reserved.
//

#import "YLCProgramPhotoInsertCell.h"
#import "YLCLateralImageShowView.h"

@interface YLCProgramPhotoInsertCell()<HXPhotoViewDelegate>
@property (strong, nonatomic) HXPhotoManager *manager;
@end
@implementation YLCProgramPhotoInsertCell
{
    UILabel *titleLabel;
    HXPhotoView *photoView;
    YLCLateralImageShowView *imageShowView;
    NSMutableArray *deleArray;
    NSArray *sourceArray;
    NSInteger allCount;
}
- (HXPhotoManager *)manager {
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _manager.configuration.photoMaxNum = 6;
    }
    return _manager;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        deleArray = [NSMutableArray array];
        
        photoView = [HXPhotoView photoManager:self.manager];
        photoView.outerCamera = YES;
        photoView.delegate = self;
        photoView.spacing = 10;
        photoView.lineCount = 4;
        photoView.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:photoView];

        titleLabel = [[UILabel alloc] init];
        
        titleLabel.text = @"案例展示";
        
        [self.contentView addSubview:titleLabel];
        
        imageShowView = [[YLCLateralImageShowView alloc] initWithFrame:CGRectZero];
        
        CGFloat itemW = (self.frameWidth- 24*MarginFit - 10 * (4 - 1)) / 4;
        
        imageShowView.cellSize = CGSizeMake(itemW, itemW);
        WS(weakSelf)
        [imageShowView setClickDelete:^(NSInteger index) {
            [weakSelf reloadCo:index];
        }];
        
        [self.contentView addSubview:imageShowView];
        
    }
    return self;
}
- (void)reloadCo:(NSInteger)index{
    
    NSMutableArray *temp = [NSMutableArray arrayWithArray:sourceArray];
    
    [temp removeObjectAtIndex:index];
    
    [deleArray addObject:[sourceArray objectAtIndex:index]];
    
    sourceArray = [NSArray arrayWithArray:temp];

    allCount = sourceArray.count;
    
    [self changeCount:allCount];
    
    [self deletePhoto:deleArray];
    
    NSMutableArray *tempImgList = [NSMutableArray arrayWithArray:_sourceList];
    
    [tempImgList removeObjectAtIndex:index];
    
    _sourceList = [NSArray arrayWithArray:tempImgList];
    
    imageShowView.isImage = YES;
    
    imageShowView.photoList = _sourceList;

}
- (void)setSourceList:(NSArray *)sourceList{
    
    allCount = sourceList.count;
    
    sourceArray = [NSArray arrayWithArray:sourceList];
    
    NSMutableArray *mua = [NSMutableArray array];
    for (NSDictionary *tempDic in sourceList) {
        [mua addObject:tempDic[@"case_img"]];
    }
    _sourceList = [NSArray arrayWithArray:mua];
    
    imageShowView.isImage = YES;
    
    imageShowView.photoList = _sourceList;
}
- (void)deletePhoto:(NSArray*)array{
    if (JK_IS_DELEGATE_RSP_SEL(self.delegate, deletePhotoWithPhotos:)) {
        [self.delegate deletePhotoWithPhotos:array];
    }
}
- (void)changeCount:(NSInteger)count{
    if (JK_IS_DELEGATE_RSP_SEL(self.delegate, changeImageCount:)) {
        [self.delegate changeImageCount:count];
    }
}
// 当view更新高度时调用
- (void)photoView:(HXPhotoView *)photoView updateFrame:(CGRect)frame{
    if (JK_IS_DELEGATE_RSP_SEL(self.delegate, changeFrameWithHeight:)) {
        [self.delegate changeFrameWithHeight:frame.size.height];
    }
}
- (void)photoView:(HXPhotoView *)photoView changeComplete:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photos videos:(NSArray<HXPhotoModel *> *)videos original:(BOOL)isOriginal{
    if (JK_IS_DELEGATE_RSP_SEL(self.delegate, insertPhotoWithPhotos:)) {
        [self.delegate insertPhotoWithPhotos:photos];
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    WS(weakSelf)

    CGFloat itemW = (self.frameWidth- 24*MarginFit - 10 * (4 - 1)) / 4;

    NSInteger li = allCount%4==0?allCount/4:(allCount/4+1);

    CGFloat top = li*(itemW+10)+55;
    
    if (li == 0) {
        top = 45;
    }
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(12*MarginFit);
        make.top.equalTo(weakSelf.mas_top).offset(10);
    }];
    
    [imageShowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_left);
        make.right.equalTo(weakSelf.mas_right).offset(-12*MarginFit);
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
        make.height.equalTo(li*(itemW+10));
    }];
    
    [photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(10);
        make.bottom.and.right.equalTo(weakSelf).offset(-10);
        make.top.equalTo(imageShowView.mas_bottom).offset(10);
    }];
}
@end
